import Vue from 'vue'
import Router from 'vue-router'

const originalPush = Router.prototype.push
Router.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const _import = (file) => require(`@/pages/${file}.vue`).default

const router = new Router({
  routes: [
    { path: '/', alias: '/login', name: 'login', component: _import('login/index'), meta: {title: '登录'} },
    { path: '/home', name: 'home', component: _import('home/index'), meta: {title: '首页'} },
    { path: '*', name: 'Error404', component: _import('404/index'), meta: {title: '404'} }
  ]
})

router.afterEach((to, from) => {
  console.log(`路由跳转：${from.name} => ${to.name}`)
  document.title = to.meta.title ? `${to.meta.title} - XUE5HEN` : 'XUE5HEN'
})

export default router
